package com.baor.service.impl;

import com.baor.dao.GgDao;
import com.baor.domain.DeviceInfo;
import com.baor.service.GgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.baor.util.UploadUtil;
import java.util.List;
@Service
public class GgServiceimpl implements GgService {
    @Autowired
    private GgDao ggDao;
    @Autowired
    private  UploadUtil uploadUtil;

    @Override
    public List<DeviceInfo> gglist() {
        List<DeviceInfo> jg = ggDao.selectList(null);
        return jg;
    }

    @Override
    public boolean updateDevice(DeviceInfo deviceInfo, MultipartFile[] imageFiles) {
        if (deviceInfo.getId() == null) {
            return false;
        }


        if (imageFiles != null && imageFiles.length > 0) {
            try {

                String imagePaths = uploadUtil.uploadUtil("/device/images", imageFiles);

                deviceInfo.setImage(imagePaths);
            } catch (Exception e) {

                return false;
            }
        }

        int row = ggDao.updateById(deviceInfo);
        return row > 0;
    }

    @Override
    public int insert(DeviceInfo deviceInfo, MultipartFile[] imageFiles) {

        DeviceInfo existingDevice = ggDao.selectById(deviceInfo.getId());
        if (existingDevice != null) {
            return -1;
        }


        if (imageFiles != null && imageFiles.length > 0) {
            try {

                String imagePaths = uploadUtil.uploadUtil("/device/images", imageFiles);

                deviceInfo.setImage(imagePaths);
            } catch (Exception e) {

                return -2;
            }
        }


        int row = ggDao.insert(deviceInfo);
        return row;
    }
}
